﻿using System;
using System.IO;
using System.Linq;
using System.Windows.Input;

namespace LFSRecord2.Model
{
    public class FileHistoryInfo
    {
        private const int TruncateLength = 4;

        public event EventHandler<FileHistoryEventArgs> OpenFile;

        public string FileName
        {
            get;
            private set;
        }

        public string Name
        {
            get;
            private set;
        }

        public ICommand OpenFileCommand
        {
            get
            {
                return new RelayCommand(param => OnOpenFile(new FileHistoryEventArgs(this)));
            }
        }

        public FileHistoryInfo(string fileName)
        {
            FileName = fileName;
            Name = TruncateLongFilePath(fileName);
        }

        private static string TruncateLongFilePath(string fileName)
        {
            string[] tokens = fileName.Split(Path.DirectorySeparatorChar);

            if (tokens.Length > (TruncateLength + 1))
            {
                string truncated = String.Join(
                    Path.DirectorySeparatorChar.ToString(),
                    tokens.Skip(tokens.Length - TruncateLength).Take(TruncateLength));

                return String.Format("{0}{1}...{1}{2}", tokens[0], Path.DirectorySeparatorChar, truncated);
            }

            return fileName;
        }

        protected virtual void OnOpenFile(FileHistoryEventArgs e)
        {
            EventHandler<FileHistoryEventArgs> temp = OpenFile;
            if (temp != null)
            {
                temp(this, e);
            }
        }
    }
}
